
import { globalUtils } from '../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import uiData from 'ui-data';
import { CurrentRunningApp } from '../../common/js/current-running-app';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewMain('lov-detail', function (scope, view) {
  var dataModel = view.getDataModel();
  var jqTable = view.$ui('lovItemTable');
  var jqCreateBtn = view.$ui('createBtn');
  var jqBackBtn = view.$ui('backBtn');
  var jqNewLovItemDialog = view.$ui('newLovItemDialog');
  var jqNewLovItemForm = jqNewLovItemDialog.find('form');
  var jqEditLovItemDialog = view.$ui('editLovItemDialog');
  var jqEditLovItemForm = jqEditLovItemDialog.find('form');

  var urlParams = view.getUrlParams();
  var valueSetId = urlParams.id;

  // 初始表格
  uiUtils.dataGrid.init(jqTable, {
    url: 'api/infrastructure/lov/pageValueItemsByCond',
    customQuery: {
      valueSetId: valueSetId
    },
    columns: [
      {
        field: 'id'
      },
      {
        field: 'valueCode'
      },
      {
        field: 'valueName'
      },
      {
        field: 'valueDesc'
      },
      {
        field: 'showOrder'
      },
      {
        field: 'enableFlagDesc'
      },
      {
        field: 'operate',
        'class': 'x-nowrap-items',
        formatter: formatOprationColumn,
        events: {
          'click .edit': clickEditOperationFn
        }
      }
    ]
  });

  function formatOprationColumn(value, row, index) {
    return [
      '<a class="edit" href="javascript:void(0)">',
      '<span>${t("ui.system.common.operation.edit")}</span>',
      '</a>'
    ].join('');
  }

  // 点击"新增"按钮的事件处理
  jqCreateBtn.on(uiUtils.event.CLICK, function () {
    // 显示弹出框
    uiUtils.dialog.showModal(jqNewLovItemDialog, {
      backdropParent: view.getViewElement(),
      onModalHide: function () {
        uiUtils.common.resetValidation(jqNewLovItemForm);
      }
    });

    // 离开页面时检查是否表单发生变更
    uiUtils.view.setCurrentFormDataToContext(scope, getNewData);
    uiUtils.view.checkFormDataChangeWhenLeave(scope, getNewData);
  });

  // 点击"返回"按钮的事件处理
  jqBackBtn.on(uiUtils.event.CLICK, function () {
    Pure.app.popView('app/infrastructure/lov/management');
  });

  var lovItemValidationRule = {
    rules: {
      valueCode: {
        required: true,
        maxlength: 50,
        customCode: true
      },
      valueName: {
        required: true,
        maxlength: 100
      },
      showOrder: {
        required: true,
        digits: true
      }
    },
    messages: {
      valueCode: {
        required: '${t("ui.infrastructure.lovDetail.validation.valueCodeRequired")}'
      },
      valueName: {
        required: '${t("ui.infrastructure.lovDetail.validation.valueNameRequired")}'
      },
      showOrder: {
        required: '${t("ui.infrastructure.lovDetail.validation.showOrderRequired")}'
      }
    }
  };

  //
  // "新建值项"弹出框
  //

  // 定义新建记录的校验规则
  jqNewLovItemForm.validate(lovItemValidationRule);

  // 点击"新建值项"弹出框的保存按钮
  jqNewLovItemDialog.on(uiUtils.event.CLICK, uiUtils.dialog.BTN_CONFIRM, function () {
    var checkResult = jqNewLovItemForm.valid();
    if (!checkResult) {
      return;
    }

    var data = dataModel.getData('new.*');
    data = uiData.utils.unprefix(data, 'new.');
    // 绑定值集ID
    data.valueSetId = valueSetId;

    // 调用保存记录的接口
    view.callService('api/infrastructure/lov/saveValueItem', data)
      .waitResult(function (saveResult) {
        if (!uiUtils.common.checkWebResultSuccess(saveResult)) {
          return;
        }

        uiUtils.dialog.hideModal(jqNewLovItemDialog);
        Pure.app.showMessage('${t("ui.system.common.msg.saveRecordSuccess")}');
        uiUtils.dataGrid.refresh(jqTable);
      });
  });

  jqNewLovItemDialog.on(uiUtils.event.MODAL_HIDE, function () {
    uiUtils.view.notCheckFormDataChangeWhenLeave(scope);
  });

  function getNewData() {
    return dataModel.getData('new.*');
  }

  //
  // "编辑值项"弹出框
  //

  jqEditLovItemForm.validate(lovItemValidationRule);

  // 点击"编辑值项"弹出框的保存按钮
  jqEditLovItemDialog.on(uiUtils.event.CLICK, uiUtils.dialog.BTN_CONFIRM, function () {
    var checkResult = jqEditLovItemForm.valid();
    if (!checkResult) {
      return;
    }

    var data = dataModel.getData('edit.*');
    data = uiData.utils.unprefix(data, 'edit.', true);

    // 调用更新记录的接口
    view.callService('api/infrastructure/lov/updateValueItem', data)
      .waitResult(function (result) {
        if (result.success) {
          uiUtils.dialog.hideModal(jqEditLovItemDialog);
          Pure.app.showMessage('${t("ui.system.common.msg.saveRecordSuccess")}');
          uiUtils.dataGrid.refresh(jqTable);
        } else {
          Pure.app.showError(result.message);
        }
      });
  });

  jqEditLovItemDialog.on(uiUtils.event.MODAL_HIDE, function () {
    uiUtils.view.notCheckFormDataChangeWhenLeave(scope);
  });

  function getEditData() {
    return dataModel.getData('edit.*');
  }

  //
  //
  //

  /**
   * 点击"编辑"操作的事件
   * @param {*} e 
   * @param {*} value 
   * @param {*} row 
   * @param {*} index 
   */
  function clickEditOperationFn(e, value, row, index) {
    uiUtils.dialog.showModal(jqEditLovItemDialog, {
      backdropParent: view.getViewElement(),
      onModalHide: function () {
        uiUtils.common.resetValidation(jqEditLovItemForm);
      }
    });

    var queryParams = {
      id: row.id
    };

    // 调用接口获取记录
    view.callService('api/infrastructure/lov/getValueItemById', queryParams)
      .waitResult(function (result, textStatus, jqXHR) {
        if (!uiUtils.common.checkWebResultSuccess(result)) {
          return;
        }

        var data = uiData.utils.prefix(result.data, 'edit.');
        dataModel.setData('edit.*', data);

        // 离开页面时检查是否表单发生变更
        uiUtils.view.setCurrentFormDataToContext(scope, getEditData);
        uiUtils.view.checkFormDataChangeWhenLeave(scope, getEditData);
      });
  }

});

/* SOURCE-CODE-END */
